package com.wy.authority.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wy.authority.entity.Msg;
import com.wy.authority.entity.SendMsg;
import com.wy.authority.service.MsgService;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.namespace.QName;
import java.util.List;

/**
 * @Author 忘忧
 * @Date Created in 2021/2/5 10:30
 * @Description:
 */
@RestController
@RequestMapping("/msg")
public class MessageController2 {
    private final static String NAMESPACE = "http://tempuri.org/";

    private final static String endpoint = "http://115.29.52.221:24663/Service/?wsdl";

    @Autowired
    private MsgService msgService;

    @ResponseBody
    @GetMapping("/find")
    public PageInfo<Msg> findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size){
        //按照排序字段 倒序 排序
        String orderBy= "id desc";
        PageHelper.startPage(page,size,orderBy);
        List<Msg> list=msgService.select();
        PageInfo<Msg> pageInfo =new PageInfo<>(list);
        return pageInfo;
    }

  //短信发送测试
    @PostMapping("/message")
    public String sendmsg(@RequestBody SendMsg sendMsg){
        System.out.println(sendMsg);
       String username=sendMsg.getUsername();
       String password=sendMsg.getPassword();
       String mobileNum=sendMsg.getMobileNum();
       String content=sendMsg.getContent();
       String productNum=sendMsg.getProductNum();
        System.out.println(SubmitSms(username,password,mobileNum,content,productNum));
        return "success";
    }
    public static String SubmitSms(String username, String password,
                                   String mobileNum, String content, String productNum) {

        String result = "";

        try {

            Service service = new Service();

            Call call = (Call) service.createCall();

            String soapActionURI = "http://tempuri.org/I_Order/SubmitSms";

            call.setUseSOAPAction(true);

            call.setSOAPActionURI(soapActionURI);

            call.setTargetEndpointAddress(endpoint);

            call.setOperationName(new QName(NAMESPACE, "SubmitSms"));// wsdl里面描述的接口名称

            call.addParameter(new QName(NAMESPACE, "username"),
                    org.apache.axis.encoding.XMLType.XSD_DATE,

                    javax.xml.rpc.ParameterMode.IN);// 接口的参数

            call.addParameter(new QName(NAMESPACE, "password"),
                    org.apache.axis.encoding.XMLType.XSD_DATE,

                    javax.xml.rpc.ParameterMode.IN);// 接口的参数

            call.addParameter(new QName(NAMESPACE, "mobileNum"),
                    org.apache.axis.encoding.XMLType.XSD_DATE,

                    javax.xml.rpc.ParameterMode.IN);// 接口的参数

            call.addParameter(new QName(NAMESPACE, "content"),
                    org.apache.axis.encoding.XMLType.XSD_DATE,

                    javax.xml.rpc.ParameterMode.IN);// 接口的参数

            call.addParameter(new QName(NAMESPACE, "productNum"),
                    org.apache.axis.encoding.XMLType.XSD_DATE,

                    javax.xml.rpc.ParameterMode.IN);// 接口的参数

            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型

            result = (String) call.invoke(new Object[] { username, password,
                    mobileNum, content, productNum });

            // 给方法传递参数，并且调用方法

        }

        catch (Exception e) {

            System.err.println(e.toString());

        }

        return result;

    }

   //订单编号模糊查询
   @ResponseBody
   @GetMapping("/selectby_ord")
   public PageInfo<Msg> selectby_ord(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size,@RequestParam(defaultValue = "GC2021") String search,@RequestParam(defaultValue = "2021") String times){
       //按照排序字段 倒序 排序
       String orderBy= "id desc";
       PageHelper.startPage(page,size,orderBy);
       List<Msg> list=msgService.selectby_ord(search,times);
       PageInfo<Msg> pageInfo =new PageInfo<>(list);
       return pageInfo;
   }
}
